package tests;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.HashMap;

import org.junit.Test;

import data.Forum;
import data.Member;
import data.MemberDataBase;

public class TestServer {
	Forum mainForum;
	MemberDataBase database;
	
	public void init(){
		mainForum =  Forum.getForum();
		mainForum.addSubForum("sport", new HashMap<Integer,Member>());
		mainForum.addSubForum("music", new HashMap<Integer,Member>());
		database = MemberDataBase.getMembersDataBase();
		database.addNewMember("admin", "admin");
		Member admin = database.getMember("admin");
		admin.set_isAdmin(true);
		mainForum.set_admin(admin);
		Member user=new Member("avi", 2);
		database.addNewMember(user.get_name(), "");
		mainForum.getSubForum(1).addThreadMsg("title", "content", user, new Date(System.currentTimeMillis()));
		}
	@Test
	public void testAddValidPostMessage() {
		init();
		assertTrue(mainForum.getSubForum(1).addPostMsg("re", "stam", database.getMember("avi"),  new Date(System.currentTimeMillis()), 1, 1));
	}

	@Test
	public void testAddValidPostMessageNoContent() {
		init();
		assertTrue(mainForum.getSubForum(1).addPostMsg("re", "", database.getMember("avi"),  new Date(System.currentTimeMillis()), 1, 1));
	}
	
	@Test
	public void testAddValidPostMessageNoTitle() {
		init();
		assertTrue(mainForum.getSubForum(1).addPostMsg("", "body", database.getMember("avi"),  new Date(System.currentTimeMillis()), 1, 1));
	}
	@Test
	public void testAddValidPostMessageWrongThread() {
		init();
		assertFalse(mainForum.getSubForum(1).addPostMsg("", "", database.getMember("avi"),  new Date(System.currentTimeMillis()), 12, 1));
	}
	@Test
	public void testEditMessageByAdmin() {
		init();
		assertTrue(mainForum.getSubForum(1).editMessage(1,"1","1","editedSub","newBody"));
		assertEquals("newBody", mainForum.getSubForum(1).getThreadMessage(1).get_content());
	}
	@Test
	public void testEditMessageByOwner() {
		init();
		assertTrue(mainForum.getSubForum(1).editMessage(2,"1","1","editedSub","newBody"));
		assertEquals("newBody", mainForum.getSubForum(1).getThreadMessage(1).get_content());
	}
	@Test
	public void testEditMessageByOtherMember() {
		init();
		Member user2=new Member("gadi", 3);
		database.addNewMember(user2.get_name(), "");
		assertFalse(mainForum.getSubForum(1).editMessage(3,"1","1","editedSub","newBody"));
	}
	@Test
	public void testEditMessageByAdminNoTitle() {
		init();
		assertTrue(mainForum.getSubForum(1).editMessage(1,"1","1","","newBody"));
		assertEquals("newBody", mainForum.getSubForum(1).getThreadMessage(1).get_content());
	}
	@Test
	public void testEditMessageByAdminNoContent() {
		init();
		assertTrue(mainForum.getSubForum(1).editMessage(1,"1","1","editedSub",""));
		assertEquals("editedSub", mainForum.getSubForum(1).getThreadMessage(1).get_title());
	}
	@Test
	public void testAddModerator() {
		init();
		assertTrue(mainForum.getSubForum(1).addModerator("avi"));
		assertTrue(database.getMember("avi").isModerator(1));
	}
	@Test
	public void testAddSubForum() {
		init();
		Member user2=new Member("shlomi", 4);
		database.addNewMember(user2.get_name(), "");
		mainForum.addSubForum("food", user2);
		assertTrue(database.getMember("shlomi").isModerator());
		
	}
	
}
